<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@include file="../base.jsp" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>    
<link href="../css/main.css" rel="stylesheet" type="text/css" >
<link rel="stylesheet" type="text/css" href="../../js/jquery/easyui1.5/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="../../js/jquery/easyui1.5/themes/icon.css">
<script type="text/javascript" src="../../js/jquery-1.8.0.min.js"></script>
<script src="../../js/jquery/easyui1.5/jquery.easyui.min.js"></script>
<script src="../../js/jquery/easyui1.5/locale/easyui-lang-zh_CN.js"></script>
<script src="../../js/myjs/myutils.js"></script>
<script type="text/javascript">

$(function(){
	treeShow();

	$("#win").dialog('close');

	$("#form_closeButton").click(function(){
		$("#win").dialog('close');
	});

	$("#form_addButton").click(function(){
		formAddEdit();
	});

});

function treeShow(){
	$('#tt1').treegrid({
		url:'menuAjax_menuTree4MenuUrls.action',
		rownumbers: true,
		idField: 'id',
		treeField:'text',
		fit: true,   //自适应大小
		columns:[[
			{title:'ID',field:'id',hidden:true,width:30},
			{title:'菜单名称',field:'text',width:200},
			{title:'URL',field:'url',width:250,align:'left'},
			{title:'排序',field:'sort',width:70,align:'center'},
			{title:'菜单类型',field:'type',width:70,align:'center'},
			{title:'是否边界访问控制',field:'hasDataLimit',width:120,align:'center'},
			{title:'图标',field:'iconClass',width:120,align:'center'},
			{field:'opt',title:'操作',width:280,align:'center',
				formatter:function(value,rowData,rowIndex){
					var type = rowData.type;
					var keyRandom = rowData.keyRandom;
					var rowData = JSON.stringify(rowData);
					rowData = encodeURI(rowData);
					rowData = encodeURI(rowData);
					var btn = "";
					if(type==30) {
						btn += "<a class=\"button_disable\" href=\"javascript:void(0)\">添加</a> ";
					}else{
						btn += "<a class=\"button\" onclick=\"addNode('" + rowData + "')\" href=\"javascript:void(0)\">添加</a> ";
					}
					btn += "<a class=\"button\" onclick=\"modifyNode('"+rowData+"')\" href=\"javascript:void(0)\">修改</a> ";
					btn += "<a class=\"button\" onclick=\"removeNode('"+rowData+"')\" href=\"javascript:void(0)\">删除</a> ";

					if(keyRandom!='') {
						btn += "<a class=\"button\" onclick=\"reCoding('" + rowData + "')\" href=\"javascript:void(0)\">重新生成源码</a> ";
					}else{
						btn += "<a class=\"button_disable\" disabled=true >重新生成源码</a> ";
					}
					return btn;
				}
			},
			{title:'keyRandom',field:'keyRandom',width:100,align:'center'}
		]],
		toolbar:[{
			id:'toolbarAddBtnId',text:'添加一级菜单',
			iconCls:'icon-add',
			handler:function(){addRootNode();}
		}],
		onClickCell:function(field,rowData){
			if(field=='text'){
				var id = rowData.id;
				$(this).treegrid('toggle', id);
			}
		},
		onLoadSuccess:function(row, data){
			//给每行后的button加easyui样式
			//applyStyle();
		}/*,
		rowStyler:function(){return "height:41px";}*/
	});
}


function addRootNode(){
	$("#win").dialog({'title':'添加框'});
	$("#win").dialog('open');
	$("#form_add").form('load',{'optType':'add','id':'0','name':'','url':'','type':'','hasDataLimit':''});
}

function reCoding(rowData){
	rowData = decodeURI(rowData);
	rowData = decodeURI(rowData);
	rowData = JSON.parse(rowData);
	var keyRandom = rowData.keyRandom;

	$.messager.prompt('重新生成源码', '将会重新生成这个菜单的源码，如果有修改过之前生成的源码，请先备份，确定继续吗？ \r\n type=1：生成web层；type=2：生成service层; type=3:生成所有 ', function(r){
		if (r){
			$.ajax({
				type:"POST",
				url:'menuAjax_recoding.action',
				dataType:"json",
				data:{'keyRandom':keyRandom,"sourceType":r},
				success:function(msg){
					messager_show(msg.responseContent,4000);
				},
				error:function(er){
					window.parent.error(er);
				}
			});
		}
	});
	/*
	confirm("将会重新生成这个菜单的源码，如果有修改过之前生成的源码，请先备份，确定继续吗？",function(){
		$.ajax({
			type:"POST",
			url:'menuAjax_recoding.action',
			dataType:"json",
			data:{'keyRandom':keyRandom},
			success:function(msg){
				messager_show(msg.responseContent,4000);
			},
			error:function(er){
				window.parent.error(er);
			}
		});
	})*/
}


function addNode(rowData){
	rowData = decodeURI(rowData);
	rowData = decodeURI(rowData);
	rowData = JSON.parse(rowData);
	$("#win").dialog({'title':'添加框'});
	$("#win").dialog('open');
	$("#form_add").form('load',rowData);
	$("#form_add").form('load',{'optType':'add','name':'','url':'','type':'','hasDataLimit':''});
}


function modifyNode(rowData){
	rowData = decodeURI(rowData);
	rowData = decodeURI(rowData);
	rowData = JSON.parse(rowData);
	//alert(rowData.text);
	$("#win").dialog({'title':'编辑框'});
	$("#win").dialog('open');
	$("#form_add").form('load',rowData);
	$("#form_add").form('load',{'optType':'edit'});
}

function formAddEdit(){
	var formAddUrl = 'menuAjax_addeditMenuNode.action';
	$('#form_add').form({
		url:formAddUrl,//参数中有id,就是修改操作，否则是添加操作
		onSubmit: function(){
			return $(this).form('validate');
		},
		success:function(data){
			var checkJsonArr = /^\{(.+:.+,*)\}$/;
			var checkResult = checkJsonArr.test(data);
			if (!checkResult)
			{
				//普通字符串处理
				var er = data;
				window.parent.error(er);
			}
			else
			{
				var _id = $("#id").val();
				var optType = $("#optType").val();
				//通过这种方法可将字符串转换为对象
				var data1 = jQuery.parseJSON(data);
				//success
				if(data1.responseCode == 1){
					//ROOT NODE
					if(_id==0){
						$("#form_add").form("clear");
						$("#win").dialog('close');
						$("#tt1").treegrid('reload');
						return;
					}
					if(optType=='add') {
						$("#tt1").treegrid('reload', _id);
					}else{
						var name = $("#name").textbox('getValue');
						var url = $("#url").textbox('getValue');
						var sort = $("#sort").textbox('getValue');
						var keyRandom = $("#keyRandom").textbox('getValue');
						var hasDataLimit = $("#hasDataLimit").combobox('getValue');
						var iconClass = $("#iconClass").combobox('getValue');

						$('#tt1').treegrid('update',{
							id: _id,
							row: {"name":name,
								"text":name,
								"url":url,
								"sort":sort,
								"keyRandom":keyRandom,
								"hasDataLimit":hasDataLimit,
								"iconClass":iconClass}
						});
					}
					$("#form_add").form("clear");
					$("#win").dialog('close');
				}else{
					$.messager.alert("操作结果",data1.responseContent);
				}
			}
		},
		error:function(er){
			window.parent.error(er);
		}
	});
	// submit the form
	$('#form_add').submit();
}

function applyStyle(){
	$('.addcls').linkbutton({plain:true,iconCls:'icon-add'});
	$('.editcls').linkbutton({plain:true,iconCls:'icon-edit'});
	$('.delcls').linkbutton({plain:true,iconCls:'icon-remove'});
	$('.recodingcls').linkbutton({plain:true,iconCls:'icon-reload'});
}

/**
 * 删除node
 * @param rowData
 */
function removeNode(rowData){

	rowData = decodeURI(rowData);
	rowData = decodeURI(rowData);
	rowData = JSON.parse(rowData);
	var menuId = rowData.id;

	confirm("删除后不可恢复，确认删除吗？",function(){
		$.ajax({
			type:"POST",
			url:'menuAjax_removeMenuNode.action',
			dataType:"json",
			data:{'id':menuId},
			success:function(msg){
				if(msg.responseCode!=1) {
					alert(msg.responseContent);
					return;
				}
				//$("#tt1").treegrid('reload');
				var nodeParent = $("#tt1").treegrid('getParent',menuId);
				//ROOT NODE
				if(nodeParent == null){
					$("#tt1").treegrid('remove',menuId);
					return;
				}
				$("#tt1").treegrid('reload', nodeParent.id);

			},
			error:function(er){
				window.parent.error(er);
			}
		});
	})
}

function confirm(msg, callback) {
	$.messager.confirm("确认框", msg, function (r) {
		if (r) {
			callback();
		}
	});
	return false;
}


</script>
	<style type="text/css">
		#form_add li{
			margin-top: 5px;
		}
		#form_add  .label{
			width: 120px;
			display: block;
			float: left;
		}
		#form_add  .input{
			float: left;
		}
		div.datagrid-cell{
			height:auto;
		}

	</style>
</head>
<body  >

<%--注：菜单类型:0=上级菜单; 10=叶子菜单; 20=页面功能; 30=访问的URL地址--%>

<div id="tt1" ></div>

<!-- 添加弹出框 添加 -->
<div id="win" class="easyui-dialog" title="添加修改数据框"　
	 style="display:none;width:550px;height:400px"
	 data-options="iconCls:'icon-save',modal:true,resizable:true"
	 buttons="#dlg-buttons">
	<form id="form_add" method="post" style="margin: 20px;">
		<input id="optType" name="optType" type="hidden" class="easyui-textbox" data-options={"width":0}>
		<input id="id" name="id" type="hidden"  class="easyui-textbox" data-options={"width":0}>
		<br/>
		<div>
			<ul>
				<li>
					<span class="label">菜单名称：</span>
					<span class="input"><input id="name" name="name" class="easyui-textbox" data-options={"width":250,required:false}></span>
					<div style="clear: both;"></div>
				</li>
				<li>
					<span class="label">URL：</span>
					<span class="input"><input id="url" name="url" class="easyui-textbox" data-options={"width":250,"height":50,"multiline":true}></span>
					<div style="clear: both;"></div>
				</li>
				<li>
					<span class="label">排序：</span>
					<span class="input"><input id="sort" name="sort" class="easyui-textbox" data-options={"width":250}></span>
					<div style="clear: both;"></div>
				</li>
				<li>
					<span class="label">keyRandom：</span>
					<span class="input"><input id="keyRandom" name="keyRandom" class="easyui-textbox" data-options={"width":250}></span>
					<div style="clear: both;"></div>
				</li>
				<li>
					<span class="label">菜单类型：</span>
					<span class="input">
						<select id="type" class="easyui-combobox" name="type" style="width:250px;" data-options={editable:false,required:true}>
							<option value="0">上级菜单</option>
							<option value="10">叶子菜单</option>
							<option value="20">页面功能按钮</option>
							<option value="30">功能按钮访问的关联URL地址</option>
						</select>
					</span>
					<div style="clear: both;"></div>
				</li>
				<li>
					<span class="label">是否边界访问控制：</span>
					<span class="input">
						<select id="hasDataLimit" class="easyui-combobox" name="hasDataLimit" style="width:250px;" data-options={editable:false,required:true}>
							<option value="0">否</option>
							<option value="1">是</option>
						</select>
					</span>
					<div style="clear: both;"></div>
				</li>
				<li>
					<span class="label">图标：</span>
					<span class="input">
						<select id="iconClass" class="easyui-combobox" name="iconClass" style="width:250px;" data-options={editable:true,required:true}>
							<option >fa fa-bullhorn Green fa-fw</option>
							<option >fa fa-bath Green fa-fw</option>
							<option >fa fa-eercast Green fa-fw</option>
							<option >fa fa-microchip Green fa-fw</option>
							<option >fa fa-handshake-o Green fa-fw</option>
							<option >fa fa-vcard Green fa-fw</option>
							<option >fa fa-ravelry Green fa-fw</option>
						</select>
					</span>
					<div style="clear: both;"></div>
				</li>
			</ul>
		</div>
	</form>
</div>


<!-- 添加弹出框 Buttons -->
<div id="dlg-buttons">
	<table cellpadding="0" cellspacing="0" style="width:100%">
		<tr>
			<td style="text-align:right">
				<a href="#" id="form_addButton" class="easyui-linkbutton" iconCls="icon-save">保存</a>
				<a href="#" id="form_closeButton" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
			</td>
		</tr>
	</table>
</div>

</body>
</html>